//
//  SN_DoneChangePwd_Controller.m
//  Shiyun_Newspaper
//
//  Created by zz on 17/6/27.
//  Copyright © 2017年 ShiYun. All rights reserved.
//

#import "SN_DoneChangePwd_Controller.h"

@interface SN_DoneChangePwd_Controller () <UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UITableViewCell *cell1;
@property (nonatomic, strong) UITableViewCell *cell2;

@property (nonatomic, strong) UITextField *field1;
@property (nonatomic, strong) UITextField *field2;


@end

@implementation SN_DoneChangePwd_Controller

#pragma mark -Getter
- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, VIEW_WIDTH, VIEW_HEIGHT - SafeAreaTopHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.scrollEnabled = false;
        UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT)];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        [footerView addGestureRecognizer:tap];

        footerView.backgroundColor = [UIColor colorWithHexString:@"#f4f5f7"];
        _tableView.tableFooterView = footerView;
        _tableView.rowHeight = 44;
        
    }
    
    return _tableView;
}

- (UITableViewCell *)cell1 {
    
    if (!_cell1) {
        
        _cell1 = [[UITableViewCell alloc] init];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 14, 100, 16)];
        label.text = @"新密码";
        label.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        label.font = [UIFont systemFontOfSize:16];
        [_cell1 addSubview:label];
        
        _field1 = [[UITextField alloc] initWithFrame:CGRectMake(92, 0, 230, 44)];
        _field1.placeholder = @"6-16位数字、字母组成";
        _field1.borderStyle = UITextBorderStyleNone;
        _field1.secureTextEntry = YES;
        _field1.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        [_cell1 addSubview:_field1];
        
        _cell1.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    return _cell1;
}

- (UITableViewCell *)cell2 {
    
    if (!_cell2) {
        _cell2 = [[UITableViewCell alloc] init];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 14, 100, 16)];
        label.text = @"确认密码";
        label.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        label.font = [UIFont systemFontOfSize:16];
        [_cell2 addSubview:label];
        
        _field2 = [[UITextField alloc] initWithFrame:CGRectMake(92, 0, 130, 44)];
        _field2.placeholder = @"再次输入新密码";
        _field2.borderStyle = UITextBorderStyleNone;
        _field2.secureTextEntry = YES;
        _field2.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        [_cell2 addSubview:_field2];
    
        
        _cell2.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    return _cell2;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navBar.navigationItem.title = @"设置新密码";
    
    UIBarButtonItem *donBtn = [[UIBarButtonItem alloc] initWithTitle:@"完成 " style:UIBarButtonItemStyleDone target:self action:@selector(donAction)];
    self.navBar.navigationItem.rightBarButtonItem = donBtn;
    [donBtn setTitleTextAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];
    [self.view addSubview:self.tableView];
}

#pragma mark -UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        
        return self.cell1;
        
    }else{
        
        return self.cell2;
    }
}


#pragma mark -Action
- (void)donAction {
    
    NSString *str1 = _field1.text;
    NSString *str2 = _field2.text;
    
    if (str1.length == 0 || str2.length == 0 ) {
        
        [Utils alertViewWithMessage:@"密码不能为空"];
        return;
    }
    
    if (str1.length<6 ||str1.length>16) {
        [Utils alertViewWithMessage:@"请将密码限制在6-16位以内"];
        return;
    }
    
    if (str2.length<6 || str2.length>16) {
        
        [Utils alertViewWithMessage:@"请将确认密码限制在6-16位以内"];
        return;
    }
    
    if (![str1 isEqualToString:str2]) {
        [Utils alertViewWithMessage:@"两次密码输入不一致"];
        return;
    }
    
    //判断是否含有标点
    NSCharacterSet *set = [NSCharacterSet punctuationCharacterSet];
    NSRange range = [str1 rangeOfCharacterFromSet:set];
    if (range.location != NSNotFound) {
        
        [Utils alertViewWithMessage:@"密码中含有标点符号"];
        return;
    }
    
    
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    
    [dic setObject:@"api_user_repass" forKey:@"Method"];
    [dic setObject:[Utils MD5EncryptWith:str1] forKey:@"newpass"];
    [dic setObject:USERID forKey:@"userid"];
    [dic setObject:[SYAccountTool ShareAccount].account.mobile forKey:@"mobile"];
    [dic setObject:self.verificationCodeStr forKey:@"verify"];

    [Utils getWithparameters:dic showHUD:YES success:^(NSInteger recode, id jsonObject) {
        
        NSString *meg=[jsonObject objectForKey:@"re_info"];
        
        //新密码储存到本地
        NSDictionary *imformation = [SyUserInfomation acquirePersonalImformationFromMemoryWithPersonID:USERID];
        NSMutableDictionary *informationM = [NSMutableDictionary dictionaryWithDictionary:imformation];
        NSMutableDictionary *userInfo = [informationM objectForKey:@"userinfo"];
        [userInfo setObject:[Utils MD5EncryptWith:str1] forKey:@"loginpass"];
        
        [informationM setObject:userInfo forKey:@"userinfo"];
        [SyUserInfomation personalImformationSaveWtih:informationM];
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:meg delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        
    } failure:^(NSInteger recode, NSError *error) {
        
        [self showHint:error.localizedDescription];
        
    }];
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)tapAction {
    
    [self.view endEditing:YES];
}


@end
